package fun.rockchen.utils.responseUtils;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * @author Rock Chen
 * @Description: 格式化返回json
 * @date 2023/10/1 15:03
 */

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@AllArgsConstructor
public class ResponseJson<T> {
    private Integer code;
    private String message;
    private T data;

    /**
     * 支持自定义的code和message
     */
    public static ResponseJson<?> json(Integer code, String message) {
        return new ResponseJson<>(code, message, null);
    }

    public static <T> ResponseJson<T> json(Integer code, String message, T data) {
        return new ResponseJson<>(code, message, data);
    }

    /**
     * 通用成功
     */
    public static ResponseJson<?> success() {
        return new ResponseJson<>(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMessage(), null);
    }

    public static <T> ResponseJson<?> success(T data) {
        return new ResponseJson<>(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMessage(), data);
    }

    /**
     * 带分页信息的通用成功
     */
//    public static <T> ResponseJson<?> pageSuccess(Long totalRecords, Integer currentPage, Integer pageSize, T data) {
//        return success(new PageInfo<>(totalRecords, currentPage, pageSize, data));
//    }

    /**
     * 通用失败
     */
    public static ResponseJson<?> fail() {
        return json(ResponseEnum.FAIL.getCode(), ResponseEnum.FAIL.getMessage());
    }

    /**
     * 账号密码失败
     */
    public static ResponseJson<?> AccountAndPasswordFail() {
        return json(ResponseEnum.LOGIN_ERROR.getCode(), ResponseEnum.LOGIN_ERROR.getMessage());
    }

    /**
     * 登录成功
     */
    public static ResponseJson<?> LoginSuccess(Object token) {
        return json(ResponseEnum.LOGIN_SUCCESS.getCode(), ResponseEnum.LOGIN_SUCCESS.getMessage(), token);
    }

    /**
     * 用户不存在
     */
    public static ResponseJson<?> NoUser() {
        return json(ResponseEnum.NO_USER.getCode(), ResponseEnum.NO_USER.getMessage());
    }

    /**
     * 未登录
     */
    public static <T> ResponseJson<T> NotLogin() {
        return json(ResponseEnum.NOT_LOGIN.getCode(), ResponseEnum.NOT_LOGIN.getMessage(), null);
    }

    /**
     * 请求成功但数据库无数据
     */
    public static ResponseJson<?> NoData() {
        return json(ResponseEnum.NO_DATA.getCode(), ResponseEnum.NO_DATA.getMessage());
    }

    /**
     * 删除数据失败
     */
    public static ResponseJson<?> deleteFail() {
        return json(ResponseEnum.DELETE_ERROR.getCode(), ResponseEnum.DELETE_ERROR.getMessage());
    }

    /**
     * 删除数据成功
     */
    public static ResponseJson<?> deleteSuccess() {
        return json(ResponseEnum.DELETE_SUCCESS.getCode(), ResponseEnum.DELETE_SUCCESS.getMessage());
    }

    /**
     * 插入数据成功
     */
    public static ResponseJson<?> insertSuccess() {
        return json(ResponseEnum.INSERT_SUCCESS.getCode(), ResponseEnum.INSERT_SUCCESS.getMessage());
    }

    /**
     * 插入数据失败
     */
    public static ResponseJson<?> insertFail() {
        return json(ResponseEnum.INSERT_ERROR.getCode(), ResponseEnum.INSERT_ERROR.getMessage());
    }

    /**
     * 修改数据失败
     */
    public static ResponseJson<?> updateFail() {
        return json(ResponseEnum.UPDATE_ERROR.getCode(), ResponseEnum.UPDATE_ERROR.getMessage());
    }

    /**
     * 修改数据成功
     */
    public static ResponseJson<?> updateSuccess() {
        return json(ResponseEnum.UPDATE_SUCCESS.getCode(), ResponseEnum.UPDATE_SUCCESS.getMessage());
    }

    /**
     * 获取数据成功
     */
    public static ResponseJson<?> getSuccess(Object data) {
        return json(ResponseEnum.GET_SUCCESS.getCode(), ResponseEnum.GET_SUCCESS.getMessage(), data);
    }

    /**
     * 获取数据失败
     */
    public static ResponseJson<?> getError() {
        return json(ResponseEnum.GET_ERROR.getCode(), ResponseEnum.GET_ERROR.getMessage());
    }

    /**
     * token过期
     *
     * @return
     */
    public static ResponseJson<?> TokenExpired() {
        return json(ResponseEnum.TOKEN_EXPIRED.getCode(), ResponseEnum.TOKEN_EXPIRED.getMessage());
    }

    /**
     * 游客登录成功
     */
    public static ResponseJson<?> visitorLoginWelcome() {
        return json(ResponseEnum.WELCOME_ACCESS.getCode(), ResponseEnum.WELCOME_ACCESS.getMessage());
    }

    /**
     * 用户创建成功
     */
    public static ResponseJson<?> userCreateSuccess(Object data) {
        return json(ResponseEnum.CREATE_USER_SUCCESS.getCode(), ResponseEnum.CREATE_USER_SUCCESS.getMessage(), data);
    }

    /**
     * 用户创建失败
     */
    public static ResponseJson<?> userCreateFail() {
        return json(ResponseEnum.CREATE_USER_FAIL.getCode(), ResponseEnum.CREATE_USER_FAIL.getMessage());
    }

    /**
     * 用户已存在
     */
    public static ResponseJson<?> userExist() {
        return json(ResponseEnum.USER_EXIST.getCode(), ResponseEnum.USER_EXIST.getMessage());
    }

    /**
     * 时间段已有任务
     */
    public static ResponseJson<?> TaskExist() {
        return json(ResponseEnum.TASK_EXIST.getCode(), ResponseEnum.TASK_EXIST.getMessage());
    }

    /**
     * 起始时间早于结束时间
     */
    public static ResponseJson<?> startTimeEarly() {
        return json(ResponseEnum.START_EARLY.getCode(), ResponseEnum.START_EARLY.getMessage());
    }
}
